#ifndef	_SOCKET_H_
#define	_SOCKET_H_

#include "types.h"

#include "w5300.h"

/**********************************************************
*
* define function of socket API 
*
***********************************************************/



uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint16 flag); // Opens a socket(TCP or UDP or IP_RAW mode)
void close(SOCKET s); // Close socket
uint8 connect(SOCKET s, uint8 * addr, uint16 port); // Establish TCP connection (Active connection)
void disconnect(SOCKET s); // disconnect the connection
uint8 listen(SOCKET s);	// Establish TCP connection (Passive connection)
uint32 send(SOCKET s, uint8 * buf, uint32 len); // Send data (TCP)
uint32 recv(SOCKET s, uint8 * buf, uint32 len);	// Receive data (TCP)
uint16 sendto(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 port); // Send data (UDP/IP RAW)
uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16  *port); // Receive data (UDP/IP RAW)
uint16 igmpsend(SOCKET s, uint8 * buf, uint16 len); // Send data (multicasting)

/**
 * It returns status of a SOCKET
 */
uint8 status(SOCKET s);

// Functions to allow buffered UDP send (i.e. where the UDP datagram is built up over a
// number of calls before being sent
/*
  @brief This function sets up a UDP datagram, the data for which will be provided by one
  or more calls to bufferData and then finally sent with sendUDP.
  @return 1 if the datagram was successfully set up, or 0 if there was an error
*/
extern int startUDP(SOCKET s, uint8_t* addr, uint16_t port);
/*
  @brief This function copies up to len bytes of data from buf into a UDP datagram to be
  sent later by sendUDP.  Allows datagrams to be built up from a series of bufferData calls.
  @return Number of bytes successfully buffered
*/
uint32_t bufferData(SOCKET s, const uint8_t* buf, uint32_t len);
/*
  @brief Send a UDP datagram built up from a sequence of startUDP followed by one or more
  calls to bufferData.
  @return 1 if the datagram was successfully sent, or 0 if there was an error
*/
int sendUDP(SOCKET s);


#endif
/* _SOCKET_H_ */
